package com.atguigu.gmall.weball.controller;


import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.feign.product.CategoryFeignClient;
import com.atguigu.gmall.model.rpc.product.BaseCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 1、引入feign依赖
 */
@Controller
public class IndexController {


    @Autowired
    CategoryFeignClient categoryFeignClient;

    /**
     * 访问首页
     * @return
     */
    @RequestMapping({"/","/index","/index.html"})
    public String index(Model model,
                        HttpServletRequest request,
                        @RequestHeader(value = RedisConstant.USER_ID_REQUEST_HEADER,required = false) String userId){

        String header = request.getHeader(RedisConstant.USER_ID_REQUEST_HEADER);
        System.out.println("web-all收到的userid头值："+header);
        System.out.println("web-all收到的@RequestHeader（USER_ID_REQUEST_HEADER）头值："+userId);
        /**
         *     prefix: classpath:/templates/
         *     suffix: .html
         *
         *
         *     classpath:/templates/index/index.html
         */
        //${list} 必须查出三级分类的数据；而且组装关系是
        //远程查询出所有分类的数据。我们得用feign远程调用
        //利用feign，给指定的微服务发送指定地址的请求
        List<BaseCategory> category = categoryFeignClient.getAllCategory();
        //给页面放入三级分类的所有数据
        model.addAttribute("list",category);
        return "index/index";
    }
}
